package com.cyt.work.kwmanagement.service.impl;

import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.model.vo.SelectVo;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.base.util.StringUtils;
import com.cyt.work.kwmanagement.dao.KwKcMessageDao;
import com.cyt.work.kwmanagement.dao.KwKcSettingDao;
import com.cyt.work.kwmanagement.dao.KwStudentMessageDao;
import com.cyt.work.kwmanagement.dao.KwTeacherSettingDao;
import com.cyt.work.kwmanagement.model.po.*;
import com.cyt.work.kwmanagement.model.vo.*;
import com.cyt.work.kwmanagement.service.*;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.wx.model.po.WxEducationUser;
import com.cyt.wx.model.vo.WxEducationUserVo;
import com.cyt.wx.service.WxEducationUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;

/**
 * 考务教师表-考务教师表Service层实现类
 *
 * @author han jun
 * @date 2019-11-26 11:26:25
 */
@Service("kwTeacherSettingService")
public class KwTeacherSettingServiceImpl extends BaseServiceImpl<KwTeacherSetting> implements KwTeacherSettingService {

    @Resource
    private KwTeacherSettingDao kwTeacherSettingDao;

    @Autowired
    private KwKcSettingService kwKcSettingService;

    @Resource
    private KwKcSettingDao kwKcSettingDao;

    @Resource
    private KwStudentMessageDao kwStudentMessageDao;

    @Resource
    private KwKcMessageDao kwKcMessageDao;


    @Autowired
    private KwTeacherSettingService kwTeacherSettingService;

    @Autowired
    private KwTeacherMessageService kwTeacherMessageService;

    @Autowired
    private WxEducationUserService wxEducationUserService;

    @Resource
    private KwKcMessageService kwKcMessageService;

    @Resource
    private KwStudentMessageService kwStudentMessageService;

    @Autowired
    public void setBaseDao(KwTeacherSettingDao kwTeacherSettingDao) {
        this.baseDao = kwTeacherSettingDao;
    }

    /**
     * 分页查询
     *
     * @param kwTeacherSettingVo 接受参数对象
     * @param pageNum 分页数
     * @param pageSize 每页显示条数
     * @return ReturnResult
     * @author han jun
     * @date 2019-11-26 11:26:25
     */
    @Override
    public ReturnResult findPage(KwTeacherSettingVo kwTeacherSettingVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        Example example = new Example(KwTeacherSetting.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        List<KwTeacherSetting> list = kwTeacherSettingDao.selectByExample(example);
        PageInfo<KwTeacherSetting> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 分页查询
     *
     * @param kwTeacherSettingVo 接受参数对象
     * @param pageNum 分页数
     * @param pageSize 每页显示条数
     * @return ReturnResult
     * @author han jun
     * @date 2019-11-26 11:26:25
     */
    @Override
    public ReturnResult findVoPage(KwTeacherSettingVo kwTeacherSettingVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<KwTeacherSettingVo> list = kwTeacherSettingDao.selectVoList(kwTeacherSettingVo);
        PageInfo<KwTeacherSettingVo> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * list查询
     *
     * @param kwTeacherSettingVo 接受参数对象
     * @return List<KwTeacherSetting>
     * @author han jun
     * @date 2019-11-26 11:26:25
     */
    @Override
    public List<KwTeacherSetting> findList(KwTeacherSettingVo kwTeacherSettingVo) {
        Example example = new Example(KwTeacherSetting.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        if (StringUtils.isNotBlank(kwTeacherSettingVo.getKsId())){
            criteria.andEqualTo("ksId", kwTeacherSettingVo.getKsId());
        }
        return kwTeacherSettingDao.selectByExample(example);
    }

    /**
     * list查询
     *
     * @param kwTeacherSettingVo 接受参数对象
     * @return List<KwTeacherSetting>
     * @author han jun
     * @date 2019-11-26 11:26:25
     */
    @Override
    public List<KwTeacherSettingVo> findVoList(KwTeacherSettingVo kwTeacherSettingVo) {
        return kwTeacherSettingDao.selectVoList(kwTeacherSettingVo);
    }

    /**
     * 单个对象查询
     *
     * @param kwTeacherSettingVo 接受参数对象
     * @return KwTeacherSetting
     * @author han jun
     * @date 2019-11-26 11:26:25
     */
    @Override
    public KwTeacherSetting findOne(KwTeacherSettingVo kwTeacherSettingVo) {
        KwTeacherSetting kwTeacherSetting = new KwTeacherSetting();
        if (StringUtils.isBlank(kwTeacherSettingVo.getId())) {
            kwTeacherSetting.setId(kwTeacherSettingVo.getId());
        }
        return kwTeacherSettingDao.selectOne(kwTeacherSetting);
    }

    /**
     * 单个对象查询
     *
     * @param kwTeacherSettingVo 接受参数对象
     * @return KwTeacherSetting
     * @author han jun
     * @date 2019-11-26 11:26:25
     */
    @Override
    public KwTeacherSettingVo findVoOne(KwTeacherSettingVo kwTeacherSettingVo) {
        return kwTeacherSettingDao.selectVoOne(kwTeacherSettingVo);
    }

    /**
     * 添加或更新
     *
     * @param kwTeacherSettingVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author han jun
     * @date 2019-11-26 11:26:25
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(KwTeacherSettingVo kwTeacherSettingVo, KwKcSettingVo kwKcSettingVo, SysUser sysUser) {
        KwKcSetting kwKcSetting = kwKcSettingService.findOne(kwKcSettingVo);
        if (kwKcSetting != null) {
            kwKcSetting.setJkNum(kwKcSettingVo.getJkNum());
            kwKcSetting.setXkNum(kwKcSettingVo.getXkNum());
            kwKcSetting.setUpdateUser(sysUser.getId());
            kwKcSetting.setUpdateTime(new Date());
            kwKcSettingDao.updateByPrimaryKey(kwKcSetting);
        }
        //删除该考务安排之前的数据
        removeAll(kwTeacherSettingVo,sysUser);
        //保存最新数据
        String[] jkId = kwTeacherSettingVo.getJkId().split(",");
        for (int i = 0; i < jkId.length; i++) {
            KwTeacherSetting kwTeacherSetting =  new KwTeacherSetting(sysUser);
            kwTeacherSetting.setKsId(kwTeacherSettingVo.getKsId());
            kwTeacherSetting.setJkId(jkId[i]);
            kwTeacherSetting.setApplyId(kwTeacherSettingVo.getApplyId());
            kwTeacherSettingDao.insert(kwTeacherSetting);
        }
        if (StringUtils.isNotBlank(kwTeacherSettingVo.getXkId())) {
            String[] xkId = kwTeacherSettingVo.getXkId().split(",");
            for (int i = 0; i < xkId.length; i++) {
                KwTeacherSetting kwTeacherSetting = new KwTeacherSetting(sysUser);
                kwTeacherSetting.setKsId(kwTeacherSettingVo.getKsId());
                kwTeacherSetting.setApplyId(kwTeacherSettingVo.getApplyId());
                kwTeacherSetting.setXkId(xkId[i]);
                kwTeacherSettingDao.insert(kwTeacherSetting);
            }
        }
        //在这里分配老师
        KwTeacherMessageVo kwTeacherMessageVo = new KwTeacherMessageVo();
        kwTeacherMessageVo.setKsId(kwTeacherSettingVo.getKsId());
        kwTeacherMessageVo.setApplyId(kwTeacherSettingVo.getApplyId());
        kwTeacherMessageService.startDistributeTeacher(kwTeacherMessageVo,sysUser);

        return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
    }




    /**
     * 根据条件逻辑删除
     *
     * @param kwTeacherSettingVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author han jun
     * @date 2019-11-26 11:26:25
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(KwTeacherSettingVo kwTeacherSettingVo, SysUser sysUser) {
        //修改字段
        KwTeacherSetting kwTeacherSetting = new KwTeacherSetting();
        kwTeacherSetting.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        kwTeacherSetting.setUpdateUser(sysUser.getId());
        kwTeacherSetting.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(KwTeacherSetting.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(kwTeacherSettingVo.getId())) {
            criteria.andIn("id", Arrays.asList(kwTeacherSettingVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        kwTeacherSettingDao.updateByExampleSelective(kwTeacherSetting, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param kwTeacherSettingVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author han jun
     * @date 2019-11-26 11:26:25
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(KwTeacherSettingVo kwTeacherSettingVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(KwTeacherSetting.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(kwTeacherSettingVo.getId())) {
            criteria.andIn("id", Arrays.asList(kwTeacherSettingVo.getId().split(",")));
        }
        kwTeacherSettingDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }


    /**
     * 根据考务id删除所有教师安排
     *
     * @param  sysUser 用户
     * @return ReturnResult
     * @author Jun Han
     * @date 2019/12/2 17:27
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult removeAll(KwTeacherSettingVo kwTeacherSettingVo, SysUser sysUser){
        KwTeacherSetting kwKmMessage = new KwTeacherSetting();
        kwKmMessage.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        kwKmMessage.setUpdateUser(sysUser.getId());
        kwKmMessage.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(KwTeacherSetting.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(kwTeacherSettingVo.getKsId())) {
            criteria.andEqualTo("ksId",kwTeacherSettingVo.getKsId());
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        kwTeacherSettingDao.updateByExampleSelective(kwKmMessage, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }






    /**
     * 根据ID获取KwTeacherSettingMap
     *
     * @param kwTeacherSettingVo 接受参数对象
     * @return KwTeacherMessage
     * @author han jun
     * @date 2019-11-26 11:25:39
     */
    @Override
    public ReturnResult getKwTeacherSettingMapById(KwTeacherSettingVo kwTeacherSettingVo){
        Map<String,String> map = new HashMap();
        KwKcSettingVo kwKcSettingVo = new KwKcSettingVo();
        kwKcSettingVo.setKsId(kwTeacherSettingVo.getKsId());
        kwKcSettingVo.setApplyId(kwTeacherSettingVo.getApplyId());
        KwKcSetting kwKcSetting = kwKcSettingService.findOne(kwKcSettingVo);
        if (kwKcSetting != null) {
            map.put("jkNum", kwKcSetting.getJkNum());
            map.put("xkNum", kwKcSetting.getXkNum());
        } else {
            map.put("jkNum", "0");
            map.put("xkNum", "0");
        }
        List<KwTeacherSettingVo> list = kwTeacherSettingService.findVoList(kwTeacherSettingVo);
        if (list != null && list.size() > 0) {
            StringBuffer jkId = new StringBuffer();
            StringBuffer jkName = new StringBuffer();
            StringBuffer xkId = new StringBuffer();
            StringBuffer xkName = new StringBuffer();
            for (int i = 0; i < list.size(); i++) {
                KwTeacherSettingVo kwTeacherSettingVO = list.get(i);
                if (StringUtils.isNotBlank(kwTeacherSettingVO.getJkId())) {
                    jkId.append(kwTeacherSettingVO.getJkId() + ",");
                    jkName.append(kwTeacherSettingVO.getJkName() + ",");
                }
                if (StringUtils.isNotBlank(kwTeacherSettingVO.getXkId())) {
                    xkId.append(kwTeacherSettingVO.getXkId() + ",");
                    xkName.append(kwTeacherSettingVO.getXkName() + ",");
                }
            }
            if (jkId.length() > 0) {
                map.put("jkId", jkId.substring(0, jkId.length() - 1));
            } else {
                map.put("jkId", "");
            }
            if (jkName.length() > 0) {
                map.put("jkName", jkName.substring(0, jkName.length() - 1));
            } else {
                map.put("jkName", "");
            }
            if (xkId.length() > 0) {
                map.put("xkId", xkId.substring(0, xkId.length() - 1));
            } else {
                map.put("xkId", "");
            }
            if (xkName.length() > 0) {
                map.put("xkName", xkName.substring(0, xkName.length() - 1));
            } else {
                map.put("xkName", "");
            }
        } else {
            map.put("jkId", "");
            map.put("jkName", "");
            map.put("xkId", "");
            map.put("xkName", "");
        }
        return new ReturnResult(map);

    }





    /**
     * 获取所有监考老师的次数
     *
     * @return ReturnResult
     * @author Jun Han
     * @date 2019/12/12 15:22
     */
    @Override
    public ReturnResult getAllTeacherJkNumer(int pageNum, int pageSize) {
        KwTeacherSettingVo kwTeacherSettingVo = new KwTeacherSettingVo();
        List<KwTeacherSettingVo> list = kwTeacherSettingDao.selectVoList(kwTeacherSettingVo);
        List<String> strings = new ArrayList<>();
        for (KwTeacherSettingVo kwTeacherSettingVo1 : list) {
            if (!StringUtils.isEmpty(kwTeacherSettingVo1.getJkId())) {
                if (strings.indexOf(kwTeacherSettingVo1.getJkId()) < 0) {
                    strings.add(kwTeacherSettingVo1.getJkId());
                }
            }
        }
        List<KwTeacherSettingVo> mapList = new ArrayList<>();
        for (String str : strings) {
            Map map = new HashMap();
            List<KwTeacherSettingVo> newList = new ArrayList<>();
            for (KwTeacherSettingVo kwTeacherSettingVo1 : list) {
                if (!StringUtils.isEmpty(kwTeacherSettingVo1.getJkId())) {
                    if (kwTeacherSettingVo1.getJkId().equals(str)) {
                        newList.add(kwTeacherSettingVo1);
                    }
                }
            }
            WxEducationUserVo wxEducationUserVo = new WxEducationUserVo();
            wxEducationUserVo.setId(str);
            WxEducationUser wxEducationUser = wxEducationUserService.listExample(wxEducationUserVo).get(0);
            KwTeacherSettingVo kwTeacherSettingnew = new KwTeacherSettingVo();
//            map.put("name", wxEducationUser.getName());
//            map.put("number", newList.size());
            kwTeacherSettingnew.setJkName(wxEducationUser.getName());
            kwTeacherSettingnew.setJkNum(String.valueOf(newList.size()));
            mapList.add(kwTeacherSettingnew);
        }

        Collections.sort(mapList, new Comparator<KwTeacherSettingVo>() {
            public int compare(KwTeacherSettingVo o1, KwTeacherSettingVo o2) {
                Integer name1 = Integer.parseInt(o1.getJkNum());//name1是从你list里面拿出来的一个
                Integer name2 =Integer.parseInt(o2.getJkNum()); //name1是从你list里面拿出来的第二个name
                return name2.compareTo(name1);
            }
        });
        for (int i =0;i<mapList.size();i++){
            KwTeacherSettingVo kwTeacherSettingVo1 = mapList.get(i);
            kwTeacherSettingVo1.setSortNumber(String.valueOf(i+1));
        }
        PageInfo<KwTeacherSettingVo> page = new PageInfo<>(mapList);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }


    /**
     * 获取所有巡考老师的次数
     *
     * @return ReturnResult
     * @author Jun Han
     * @date 2019/12/12 15:22
     */
    @Override
    public ReturnResult getAllTeacherXkNumer(int pageNum, int pageSize) {
        KwTeacherSettingVo kwTeacherSettingVo = new KwTeacherSettingVo();
        List<KwTeacherSettingVo> list = kwTeacherSettingDao.selectVoList(kwTeacherSettingVo);
        List<String> strings = new ArrayList<>();
        for (KwTeacherSettingVo kwTeacherSettingVo1 : list) {
            if (!StringUtils.isEmpty(kwTeacherSettingVo1.getXkId())) {
                if (strings.indexOf(kwTeacherSettingVo1.getXkId()) < 0) {
                    strings.add(kwTeacherSettingVo1.getXkId());
                }
            }
        }
        List<KwTeacherSettingVo> mapList = new ArrayList<>();
        for (String str : strings) {
            Map map = new HashMap();
            List<KwTeacherSettingVo> newList = new ArrayList<>();
            for (KwTeacherSettingVo kwTeacherSettingVo1 : list) {
                if (!StringUtils.isEmpty(kwTeacherSettingVo1.getXkId())) {
                    if (kwTeacherSettingVo1.getXkId().equals(str)) {
                        newList.add(kwTeacherSettingVo1);
                    }
                }
            }
            WxEducationUserVo wxEducationUserVo = new WxEducationUserVo();
            wxEducationUserVo.setId(str);
            WxEducationUser wxEducationUser = wxEducationUserService.listExample(wxEducationUserVo).get(0);
            KwTeacherSettingVo kwTeacherSettingnew = new KwTeacherSettingVo();
            kwTeacherSettingnew.setXkName(wxEducationUser.getName());
            kwTeacherSettingnew.setXkNum(String.valueOf(newList.size()));
            //设置排名的情况
            mapList.add(kwTeacherSettingnew);
        }

        Collections.sort(mapList, new Comparator<KwTeacherSettingVo>() {
            public int compare(KwTeacherSettingVo o1, KwTeacherSettingVo o2) {
                Integer name1 = Integer.parseInt(o1.getXkNum());//name1是从你list里面拿出来的一个
                Integer name2 =Integer.parseInt(o2.getXkNum()); //name1是从你list里面拿出来的第二个name
                return name2.compareTo(name1);
            }
        });
       for (int i =0;i<mapList.size();i++){
           KwTeacherSettingVo kwTeacherSettingVo1 = mapList.get(i);
           kwTeacherSettingVo1.setSortNumber(String.valueOf(i+1));
       }
        PageInfo<KwTeacherSettingVo> page = new PageInfo<>(mapList);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }


    /**
     * 获取所有巡考老师的次数<List>
     *
     * @return ReturnResult
     * @author Jun Han
     * @date 2019/12/12 15:22
     */
    @Override
    public  List<KwTeacherExportVo> getAllExportXKteacher(){
        KwTeacherSettingVo kwTeacherSettingVo = new KwTeacherSettingVo();
        List<KwTeacherSettingVo> list = kwTeacherSettingDao.selectVoList(kwTeacherSettingVo);
        List<String> strings = new ArrayList<>();
        for (KwTeacherSettingVo kwTeacherSettingVo1 : list) {
            if (!StringUtils.isEmpty(kwTeacherSettingVo1.getXkId())) {
                if (strings.indexOf(kwTeacherSettingVo1.getXkId()) < 0) {
                    strings.add(kwTeacherSettingVo1.getXkId());
                }
            }
        }
        List<KwTeacherExportVo> mapList = new ArrayList<>();
        for (String str : strings) {
            Map map = new HashMap();
            List<KwTeacherSettingVo> newList = new ArrayList<>();
            for (KwTeacherSettingVo kwTeacherSettingVo1 : list) {
                if (!StringUtils.isEmpty(kwTeacherSettingVo1.getXkId())) {
                    if (kwTeacherSettingVo1.getXkId().equals(str)) {
                        newList.add(kwTeacherSettingVo1);
                    }
                }
            }
            WxEducationUserVo wxEducationUserVo = new WxEducationUserVo();
            wxEducationUserVo.setId(str);
            WxEducationUser wxEducationUser = wxEducationUserService.listExample(wxEducationUserVo).get(0);
            KwTeacherExportVo kwTeacherExportVo = new KwTeacherExportVo();
            kwTeacherExportVo.setName(wxEducationUser.getName());
            kwTeacherExportVo.setXkNum(String.valueOf(newList.size()));
            //设置排名的情况
            mapList.add(kwTeacherExportVo);
        }

        Collections.sort(mapList, new Comparator<KwTeacherExportVo>() {
            public int compare(KwTeacherExportVo o1, KwTeacherExportVo o2) {
                Integer name1 = Integer.parseInt(o1.getXkNum());//name1是从你list里面拿出来的一个
                Integer name2 =Integer.parseInt(o2.getXkNum()); //name1是从你list里面拿出来的第二个name
                return name1.compareTo(name2);
            }
        });
        for (int i =0;i<mapList.size();i++){
            KwTeacherExportVo kwTeacherExportVo = mapList.get(i);
            kwTeacherExportVo.setSortNumber(String.valueOf(i+1));
        }

        return mapList;

    }

    /**
     * 获取所有监考老师的次数<List>
     *
     * @return ReturnResult
     * @author Jun Han
     * @date 2019/12/12 15:22
     */
    @Override
    public List<KwTeacherJkStaticVo> getAllExportJKteacher(){
        KwTeacherSettingVo kwTeacherSettingVo = new KwTeacherSettingVo();
        List<KwTeacherSettingVo> list = kwTeacherSettingDao.selectVoList(kwTeacherSettingVo);
        List<String> strings = new ArrayList<>();
        for (KwTeacherSettingVo kwTeacherSettingVo1 : list) {
            if (!StringUtils.isEmpty(kwTeacherSettingVo1.getJkId())) {
                if (strings.indexOf(kwTeacherSettingVo1.getJkId()) < 0) {
                    strings.add(kwTeacherSettingVo1.getJkId());
                }
            }
        }
        List<KwTeacherJkStaticVo> mapList = new ArrayList<>();
        for (String str : strings) {
            Map map = new HashMap();
            List<KwTeacherSettingVo> newList = new ArrayList<>();
            for (KwTeacherSettingVo kwTeacherSettingVo1 : list) {
                if (!StringUtils.isEmpty(kwTeacherSettingVo1.getJkId())) {
                    if (kwTeacherSettingVo1.getJkId().equals(str)) {
                        newList.add(kwTeacherSettingVo1);
                    }
                }
            }
            WxEducationUserVo wxEducationUserVo = new WxEducationUserVo();
            wxEducationUserVo.setId(str);
            WxEducationUser wxEducationUser = wxEducationUserService.listExample(wxEducationUserVo).get(0);
            KwTeacherJkStaticVo kwTeacherJkStaticVo = new KwTeacherJkStaticVo();
            kwTeacherJkStaticVo.setName(wxEducationUser.getName());
            kwTeacherJkStaticVo.setXkNum(String.valueOf(newList.size()));
            mapList.add(kwTeacherJkStaticVo);
        }

        Collections.sort(mapList, new Comparator<KwTeacherJkStaticVo>() {
            public int compare(KwTeacherJkStaticVo o1, KwTeacherJkStaticVo o2) {
                Integer name1 = Integer.parseInt(o1.getXkNum());//name1是从你list里面拿出来的一个
                Integer name2 =Integer.parseInt(o2.getXkNum()); //name1是从你list里面拿出来的第二个name
                return name1.compareTo(name2);
            }
        });
        for (int i =0;i<mapList.size();i++){
            KwTeacherJkStaticVo kwTeacherJkStaticVo = mapList.get(i);
            kwTeacherJkStaticVo.setSortNumber(String.valueOf(i+1));
        }
        return mapList;
    }

    /**
     * 获监巡考老师选择器
     *
     * @param wxEducationUserVo 筛选条件
     * @return ReturnResult
     * @author Jun Han
     * @date 2019/9/26 15:59
     */
    @Override
    public ReturnResult selectorjxk(WxEducationUserVo wxEducationUserVo,String type,String ksId){
        KwTeacherSettingVo kwTeacherSettingVo = new KwTeacherSettingVo();
        kwTeacherSettingVo.setKsId(ksId);
        List<KwTeacherSetting> list = findList(kwTeacherSettingVo);
        List<WxEducationUser> newList = new ArrayList<>();
        if (type.equals("1")){//监考的
            for (KwTeacherSetting kwTeacherSetting:list){
                if (StringUtils.isNotBlank(kwTeacherSetting.getJkId())){
                    WxEducationUserVo wxEducationUserVonew = new WxEducationUserVo();
                    wxEducationUserVonew.setId(kwTeacherSetting.getJkId());
                    wxEducationUserVonew.setName(wxEducationUserVo.getName());
                    List<WxEducationUser> wxList = wxEducationUserService.listExample(wxEducationUserVonew);
                    if(wxList!=null&&wxList.size()>0) {
                        newList.add(wxList.get(0));
                    }
                }
            }

        }else {
            for (KwTeacherSetting kwTeacherSetting:list){
                if (StringUtils.isNotBlank(kwTeacherSetting.getXkId())){
                    WxEducationUserVo wxEducationUserVonew = new WxEducationUserVo();
                    wxEducationUserVonew.setId(kwTeacherSetting.getXkId());
                    wxEducationUserVonew.setName(wxEducationUserVo.getName());
                    List<WxEducationUser> wxList = wxEducationUserService.listExample(wxEducationUserVonew);
                    if(wxList!=null&&wxList.size()>0) {
                        newList.add(wxList.get(0));
                    }
                }
            }
        }
        List<SelectVo> selectVoList=new ArrayList<>();
        if(newList!=null&&newList.size()>0){
            for(WxEducationUser wxEducationUser :newList){
                List<String> showNameList=new ArrayList<>();
                showNameList.add(wxEducationUser.getName());
                showNameList.add(wxEducationUser.getUserNumber());
                selectVoList.add(new SelectVo(wxEducationUser.getId(), wxEducationUser.getName(),showNameList));
            }
        }
        return new ReturnResult(selectVoList);
    }


    /**
     * 获取某个考场所有的座位号
     *
     * @return ReturnResult
     * @author Jun Han
     * @date 2019/9/26 15:59
     */
    @Override
    public ReturnResult selectorStudentSeatNum(String kcId, String ksId){
        KwStudentMessageVo kwStudentMessageVo = new KwStudentMessageVo();
        kwStudentMessageVo.setKcId(kcId);
        kwStudentMessageVo.setKsId(ksId);
        List<KwStudentMessageVo>list  = kwStudentMessageDao.selectVoNewList(kwStudentMessageVo);
        List<SelectVo> selectVoList=new ArrayList<>();
        if(list!=null&&list.size()>0){
            for(KwStudentMessageVo kwStudentNew :list){
                List<String> showNameList=new ArrayList<>();
                showNameList.add(kwStudentNew.getKcNum());
                showNameList.add(kwStudentNew.getSeatNum());
                selectVoList.add(new SelectVo(kwStudentNew.getId(), kwStudentNew.getName(),showNameList));
            }
        }
        return new ReturnResult(selectVoList);

    }


    /**
     * 更新保存座位信息
     *
     * @return ReturnResult
     * @author Jun Han
     * @date 2019/9/26 15:59
     */
    @Override
    public ReturnResult addUpdateStudentSeatNum(String newId,String id,SysUser sysUser){
        //更新替换
        KwStudentMessageVo kwStudentMessageVo = new KwStudentMessageVo();
        kwStudentMessageVo.setId(id);
        KwStudentMessageVo kwStudentMessageVoNew = new KwStudentMessageVo();
        kwStudentMessageVoNew.setId(newId);
        List<KwStudentMessage> list = kwStudentMessageService.listExample(kwStudentMessageVo);
        List<KwStudentMessage> listNew = kwStudentMessageService.listExample(kwStudentMessageVoNew);
        if (list.size()>0){
            KwStudentMessage kwStudentMessage = list.get(0);//这是原来的
            KwStudentMessage kwStudentMessageNew = listNew.get(0);//这是是新的

            kwStudentMessage.setSeatNum(kwStudentMessageNew.getSeatNum());
            kwStudentMessage.setStSeatNum(kwStudentMessageNew.getStSeatNum());
            kwStudentMessage.setExamNum(kwStudentMessageNew.getExamNum());
            kwStudentMessage.setUpdateUser(sysUser.getId());
            kwStudentMessage.setUpdateTime(new Date());

            List<KwStudentMessage> listother = kwStudentMessageService.listExample(kwStudentMessageVo);
            KwStudentMessage kwStudentMessageother = listother.get(0);
            kwStudentMessageNew.setSeatNum(kwStudentMessageother.getSeatNum());
            kwStudentMessageNew.setStSeatNum(kwStudentMessageother.getStSeatNum());
            kwStudentMessageNew.setExamNum(kwStudentMessageother.getExamNum());
            kwStudentMessageNew.setUpdateUser(sysUser.getId());
            kwStudentMessageNew.setUpdateTime(new Date());

            kwStudentMessageDao.updateByPrimaryKey(kwStudentMessageNew);
            kwStudentMessageDao.updateByPrimaryKey(kwStudentMessage);
        }
        return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);

    }

}